package ch.fhnw.ams.sudoku.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import ch.fhnw.ams.sudoku.Globals;

/**
 * Represents the user.properties file in the user home directory.
 * 
 * @author amsler
 *
 */
public class UserProperties {
	
	public final static String USER_PROPERTIES_PATH = Globals.USER_HOME_DIR
			+ Globals.SEP + "user.properties";
	
	private final static String LAST_GAME_KEY = "last.game";
	private final static String LAST_EXPORT_DIR_KEY = "last.export.dir";
	
	private static Logger log = Logger.getLogger(UserProperties.class);
	private static UserProperties singleInstance;
	
	private final Properties properties;
	
	
	
	private UserProperties() {
		properties = new Properties();
	}
	
	public static UserProperties getInstance() {
		if (singleInstance == null) {
			singleInstance = new UserProperties();
			singleInstance.load();
		}
		
		return singleInstance;
	}

	private void load() {
		File userPropFile = new File(USER_PROPERTIES_PATH);
		
		if (!userPropFile.exists()) {
			return;
		}
		
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(
					new FileInputStream(USER_PROPERTIES_PATH));
			properties.load(in);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
	}
	
	/**
	 * Ssave all properties.
	 */
	public void save() {
		BufferedOutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(USER_PROPERTIES_PATH));
			properties.store(out, "user properties");
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			try {
				if (out != null) {
					out.flush();
					out.close();
				}
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
	}
	
	/**
	 * Returns the last game played by the user.
	 * 
	 * @return the file path of the last played game.
	 */
	public String getLastGame() {
		return properties.getProperty(LAST_GAME_KEY, "");
	}
	
	/**
	 * Sets the last played game by the user.
	 * 
	 * @param gameId the file path of the last played game.
	 */
	public void setLastGame(String gameId) {
		properties.setProperty(LAST_GAME_KEY, gameId);
	}
	
	/**
	 * Gets the directory where the user saved the image.
	 * 
	 * @return the file path of the last export directory.
	 */
	public String getLastExportDir() {
		return properties.getProperty(LAST_EXPORT_DIR_KEY, "");
	}
	
	/**
	 * Set the directory where the user saved the image.
	 * 
	 * @param lastExportDir the file path of the last export directory.
	 */
	public void setLastExportDir(String lastExportDir) {
		properties.setProperty(LAST_EXPORT_DIR_KEY, lastExportDir);
	}
}
